
//
//  HYVisitorView.swift
//  WeiBo
//
//  Created by crazyfish on 16/3/11.
//  Copyright © 2016年 crazyfish. All rights reserved.
//

import UIKit
import SnapKit

//1.创建协议
@objc protocol HYVisitorViewDelegate: NSObjectProtocol{
    
   optional func visitorView(visitorView: HYVisitorView,loginButton: UIButton)
    
}

class HYVisitorView: UIView {
    //如果在swift中用weak修饰代理,需要继承及协议nsobjectprotocl
//2.设置代理属性
    weak var delegate: HYVisitorViewDelegate?

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        startAnimation()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置访客视图的方法
    
    func setpageInfo(imageNamed: String?, message: String?){
        
        if imageNamed == nil{
            
            //如果是首页
            startAnimation()
            
        }else{
            
            //如果不是首页
            cirleView.hidden = true
            iconView.image = UIImage(named: imageNamed!)
            messageLabel.text = message!

        }
        
    }
    
    private func setupUI(){
        //设置访客视图view的方法
        //设置视图背景色
        backgroundColor = UIColor(white: 237/255, alpha: 1)
        //添加子控件
        addSubview(cirleView)
        addSubview(maskIconView)
        addSubview(iconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        
        
        //给子控件添加约束
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        cirleView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(iconView.snp_center)
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.width.equalTo(224)
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(cirleView.snp_bottom)
            
        }
        registerButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(messageLabel)
            make.width.equalTo(100)
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
        }
        loginButton.snp_makeConstraints { (make) -> Void in
            
            make.trailing.equalTo(messageLabel)
            make.top.equalTo(registerButton.snp_top)
            make.width.equalTo(registerButton.snp_width)
        }
        maskIconView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(cirleView.snp_center)
            
        }
        
        
        
    }
    
    
    //懒加载子控件
    
    //房子
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //圆圈
    private lazy var cirleView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //消息label
    private lazy var messageLabel: UILabel = {
    
         let label = UILabel(textColor: UIColor.grayColor(), fontSize:14)
        
         label.textAlignment = .Center
        
         label.numberOfLines = 0
        
         label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        
         return label
    
    }()
    
    //注册按钮
    private lazy var registerButton: UIButton = {
     
      let button = UIButton()
        
       button.addTarget(self, action: "loginBtnClick:", forControlEvents: .TouchUpInside)
        
       button.setBackgroundImage(UIImage(named: "common_button_white"), forState: .Normal)
        
       button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        //给按钮设置颜色的时候一定是用set方法
       button.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        
       button.setTitle("注册", forState: .Normal)
        
       return button
        
    }()
    
    //登录按钮
    private lazy var loginButton: UIButton = {
    
       let button = UIButton()
        
        button.addTarget(self, action: "loginBtnClick:", forControlEvents: .TouchUpInside)

        button.setBackgroundImage(UIImage(named: "common_button_white"), forState: .Normal)

        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        
        button.setTitle("登录", forState: .Normal)
        
        return button
        
    
    }()
    
    //3.让代理执行的方法
    @objc private func loginBtnClick(button: UIButton){
        
        delegate?.visitorView?(self, loginButton: button)
        
    }

    
    //遮罩图片
    private lazy var maskIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))

    //这只圈旋转的动画
    
    private func startAnimation(){
        
        //基础动画
       let anim = CABasicAnimation(keyPath: "transform.rotation")
        //指定动画的结束点
        anim.toValue = 2 * M_PI
        //重复次数
        anim.repeatCount = MAXFLOAT
        //动画的时长
        anim.duration = 20
        //在切换界面或者应用退出后台会停用
        anim.removedOnCompletion = false
        //添加动画
        cirleView.layer.addAnimation(anim, forKey: nil)
        
        }

}
